package cn.zymall.common.utils.time;

import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author xuxianda
 * @Description: 时间工具类
 * @date 2020/8/19 17:59
 * @Copyright
 * @return
 **/
@Component
public class TimeUtil {
    /**
     * 时间转字符串
     * @param date
     * @return
     */
    public String formatDateToCommon(Date date) {
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formate.format(date);
    }

    /**
     * @Description 获取当前时间
     * @return
     */
    public Date getSystemDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf.parse(sdf.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @Description: 获取系统当前时间
     * @return
     */
    public String getSystemDateSring(){
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formate.format(new Date());
    }

    /**
     *
     * @Title：StringToDate
     * @Description: 字符串转换成日期
     * @param dateStr
     * @return
     * @throws ParseException
     */
    public Date stringToDate(String dateStr) throws ParseException {
        DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=null;
        date = sdf.parse(dateStr);
        return date;
    }

    /**
     *
     * @Title：getMillForDateTimeDouble
     * @Description: 获取两个时间之间的毫秒数
     * @return
     */
    public long getMillForDateTimeDouble(Date startTime, Date endTime) {
        long lTime = startTime.getTime();
        long eTime = endTime.getTime();
        long s = eTime - lTime ;
        return s;
    }

    /**
     *
     * @Title：formatDuring
     * @Description: 将毫秒数转换为时分秒
     * @param mss
     * @return
     */
    public String formatDuring(long mss) {
        long days = mss / (1000 * 60 * 60 * 24);
        long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
        long seconds = (mss % (1000 * 60)) / 1000;
        return days + " 天 " + hours + " 时 " + minutes + " 分 "
                + seconds + " 秒 ";
    }

    /**
     * 计算两个日期之间相差的天数
     * @param format yyyyMMdd
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public int daysBetween(String format,Date smdate,Date bdate) throws ParseException
    {
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        smdate=sdf.parse(sdf.format(smdate));
        bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }
}
